//
//  main.c
//  C_Study
//
//  Created by rui on 12-6-30.
//  Copyright (c) 2012年 Wang Rui. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <unistd.h> 

/*
 @要点：静态栈
 */

typedef unsigned int bool;
#define true 1;
#define false 0;

#undef DEBUG
#define DEBUG 1


typedef struct Stack{
	int bottom;
	int top;
	int length;
	int *pBase;
	
}STACK, *PSTACk;

bool is_empty(PSTACk pStack){
	assert(NULL != pStack);
	return pStack->top == pStack->bottom;
}

bool is_full(PSTACk pStack){
	assert(NULL != pStack);
	return pStack->top == pStack->length-1;
}


void init(PSTACk pStack,int len){
	assert(NULL != pStack);
	pStack->pBase = (int *)malloc(sizeof(int)*len);
	
	if (NULL == pStack->pBase) {
		exit(-1);
	}
	
	pStack->bottom = pStack->top = -1;
	pStack->length = len;
}

bool push(PSTACk pStack,int val){
	assert(NULL != pStack);
	if (is_full(pStack)) {
		return false;
	}
	
	pStack->top ++;
	pStack->pBase[pStack->top] = val;
	
	return true;
}


bool pop(PSTACk pStack,int *popVal){
	assert(NULL != pStack && NULL != popVal);
	if (is_empty(pStack)) {
		return false;
	}
	
	*popVal = pStack->pBase[pStack->top];
	pStack->top --;
	
	return true;
}

void travel_stack(PSTACk pStack){
	assert(NULL != pStack);
	printf("stack:");
	
	int p = pStack->top;
	while (p != pStack->bottom) {
		printf("%d ",pStack->pBase[p]);
		p--;
	}
	
	printf("\n");
}

/**********************************
 *@ main函数
 **********************************/
int main(int argc, const char * argv[])
{
	STACK s;
	int val;
	init(&s, 3);
	
	push(&s, 1);
	push(&s, 2);
	push(&s, 3);

	travel_stack(&s);
	
	pop(&s, &val);
	printf("pop:%d\n",val);
	travel_stack(&s);
	
	pop(&s, &val);
	printf("pop:%d\n",val);
	travel_stack(&s);
	
	push(&s, 17);
	travel_stack(&s);
	
	return 0;
}

